var express = require('express');
const Result = require('../lib/Result');
const isRule = require('../util/isRule');
const Skus = require('../db/models/skus/skus');
var router = express.Router();
/**
 * 修改商品规格状态,81,15,6
 */
router.post("/:id/update_status",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [81, 15, 6], res, token.super)) {
        let status=req.body.status
        let id=req.params.id
        Skus.updateStatus(status,id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
/**
 * 批量删除商品规格,79,15,6
 */
router.post("/delete_all",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [79, 15, 6], res, token.super)) {
        let ids=req.body.ids
        Skus.deletebyIds(ids).then(value=>{
            res.json(Result.success(value))
        })
    }
})
/**
 * 修改商品规格,80,15,6
 */
router.post("/:id",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [80, 15, 6], res, token.super)) {
        let status=req.body.status
        let name=req.body.name
        let order=req.body.order
        let default1=req.body.default
        Skus.updateSkus(status,name,order,default1,req.params.id).then(value=>{
            res.json(Result.success(true))
        })
    }
})
/**
 * 增加商品规格,78,15,6
 */
router.post("",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [78, 15, 6], res, token.super)) {
        let status=req.body.status
        let name=req.body.name
        let order=req.body.order
        let default1=req.body.default1
        Skus.addSkus(status,name,order,default1).then(value=>{
            res.json(Result.success(value.toJSON()))
        })
    }
})

/**
 * 获取商品规格列表,15,6
 */
router.get("/:page",(req,res)=>{
    let token = req.auth
    if (isRule(token.rule, [15, 6], res, token.super)) {
        let limit=req.query.limit?req.query.limit:10
        let page=req.params.page
        Skus.getSkus(Number(page),Number(limit)).then(value=>{
            res.json(Result.success(value))
        })
    }
})
module.exports=router